package com.infore.dataReceiving.util;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.infore.dataReceiving.core.chain.Air212InstructionsChain;
import com.infore.dataReceiving.core.enums.Air212InstructionsRegularExpressionEnum;
import com.infore.dataReceiving.core.enums.Instructions212RegularExpressionEnum;
import com.infore.dataReceiving.core.filter.instructions.air212.RtdFilter;
import com.infore.dataReceiving.dto.AirInstructionsDto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;

public class Instructions212HandleUtil {
	
	public static Map<String, ChannelHandlerContext> cMap = new HashMap<>();
	
	public static Map<ChannelId, String> channelMap = new HashMap<>();
	
	public static Map<String, Map<String, Object>> dateMap = new HashMap<>();
	
	private static final ThreadLocal<SimpleDateFormat> sdfThreadLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };
	
	public static AirInstructionsDto getAirInstructions(String msg) {
		Pattern p=Pattern.compile(Instructions212RegularExpressionEnum.Air212_Instructions.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.matches();
		AirInstructionsDto airInstructionsDto = null;
		if(_flag) {
			airInstructionsDto = new AirInstructionsDto();
			airInstructionsDto.setHead("##");
			airInstructionsDto.setDataLength(m.group(1)==null?null:Integer.valueOf(m.group(1)));
			airInstructionsDto.setDatas(m.group(2));
			airInstructionsDto.setSt(m.group(3));
			airInstructionsDto.setCn(m.group(4));
			airInstructionsDto.setPw(m.group(5));
			airInstructionsDto.setMn(m.group(6));
			airInstructionsDto.setDataTime(m.group(7));
			airInstructionsDto.setFactorData(m.group(8));
			airInstructionsDto.setCrc(m.group(9));
			airInstructionsDto.setEnd("\r\n");
		}
		return airInstructionsDto;
	}
	
	public static Map<String, String> getInstructions_Flag(String msg) {
		Pattern p=Pattern.compile(Air212InstructionsRegularExpressionEnum.Air212_Instructions_Flag.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		Map<String, String> map = null;
		if(_flag) {
			map = new HashMap<String, String>();
			map.put("factorCode", m.group(1));
			map.put("flag", m.group(2));
		}
		return map;
	}
	
	public static String getInstructions_Rtd(String msg) {
		Pattern p=Pattern.compile(Air212InstructionsRegularExpressionEnum.Air212_Instructions_Rtd.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		String value = null;
		if(_flag) {
			value = m.group(1);
		}
		return value;
	}
	
	public static Map<String, String> getInstructions_ProjectCode(String msg) {
		Pattern p=Pattern.compile(Air212InstructionsRegularExpressionEnum.Air212_Instructions_ProjectCode.getParrtern()); 
		Matcher m=p.matcher(msg);
		boolean _flag = m.find();
		Map<String, String> map = null;
		if(_flag) {
			map = new HashMap<String, String>();
			map.put("factorCode", m.group(1));
			map.put("value", m.group(2));
		}
		return map;
	}
	
	
	public static Timestamp getTimestamp(String dataTime) {
		if(!dataTime.contains("-")) {
			dataTime = dataTime.substring(0,4)+"-"+dataTime.substring(4,6)+"-"+dataTime.substring(6,8)
			+" "+dataTime.substring(8,10)+":"+dataTime.substring(10,12)+":"+dataTime.substring(12,14);
		}
		SimpleDateFormat sdf = sdfThreadLocal.get();
		Date date = null;
		try {
		date = sdf.parse(dataTime);
		} catch (ParseException e) {
		e.printStackTrace();
		}
		return new Timestamp(date.getTime());
	}
	
	public static Air212InstructionsChain getMessageFilterChain() {
		Air212InstructionsChain chain = new Air212InstructionsChain();
		chain.addFilter(new RtdFilter());
		return chain;
	}
	
	public static String getDataLengthString(String dataLength) {
		if(dataLength.length()==1) {
			dataLength="000"+dataLength;
		}else if(dataLength.length()==2) {
			dataLength="00"+dataLength;
		}else if(dataLength.length()==3) {
			dataLength="0"+dataLength;
		}
		return dataLength;
	}

}
